home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / jukbxext.lha / JukeBox-Extensions / source / prog-gui.c < prev   
C/C++ Source or Header  |  1996-01-02  |  11KB  |  482 lines

  1. /* JB-Prog-Gui -- macht die JukeBox programmierbar */
  2.  
  3. /* INCLUDE */
  4.  
  5. #include <exec/lists.h>
  6. #include <exec/memory.h>
  7. #include <intuition/intuition.h>
  8. #include <libraries/gadtools.h>
  9. #include <clib/alib_protos.h>
  10. #if defined(_DCC)
  11.   #include <clib/intuition_protos.h>
  12.   #include <clib/graphics_protos.h>
  13.   #include <clib/gadtools_protos.h>
  14.   #include <clib/exec_protos.h>
  15.   #include <clib/dos_protos.h>
  16. #else
  17.   #include <proto/intuition.h>
  18.   #include <proto/graphics.h>
  19.   #include <proto/gadtools.h>
  20.   #include <proto/exec.h>
  21.   #include <proto/dos.h>
  22. #endif
  23. #include <string.h>
  24.  
  25.  
  26. /* P R O T O T Y P E N */
  27.  
  28.  
  29. void WriteNumsToFile(void);
  30. struct Node *FindNodeNum(struct List *,UWORD);
  31. void ChangeRightGadget(void);
  32. void AddNodeRight(UWORD);
  33. void RemNodeRight(UWORD);
  34. void AdjustGadgets(void);
  35. int MakeGadgets(void);
  36. void RemoveGadgets(void);
  37. int MakeList(void);
  38. struct Node *MakeNode(char *);
  39. void ShowMsg(STRPTR);
  40.  
  41. #define POOLSIZE     4096
  42. #define TRESHSIZE    1024
  43.  
  44. #define DEFAULTIN  "t:jb.toc"
  45. #define DEFAULTOUT "t:jb.tracks"
  46. #define DEFAULTLEFT "Inhalt"
  47. #define DEFAULTRIGHT "gewählt"
  48. #define NOMEM        "Zuwenig Speicher!"
  49.  
  50. /* Globale Variablen und Strukturen */
  51.  
  52. const char VERSION[]="$VER: Prog-GUI V2.00 by P. Kursawe 1996";
  53.  
  54. /* libnix specials */
  55.  
  56. int __nocommandline = 1;
  57. int __initlibraries = 1;
  58.  
  59. struct TagItem gg1tags[]={GTLV_Labels,0,TAG_DONE,0};
  60. struct TagItem gg2tags[]={GTLV_Labels,0,GTLV_Top,0,TAG_DONE,0};
  61.  
  62. struct GfxBase *GfxBase = NULL;
  63. struct IntuitionBase *IntuitionBase = NULL;
  64. struct Library *GadToolsBase=NULL;
  65. struct DosLibrary *DOSBase= NULL;
  66. struct Gadget *gg1ptr = NULL;
  67. struct Gadget *gg2ptr = NULL;
  68. struct Gadget *glistptr = NULL;
  69. struct Gadget *context = NULL;
  70. struct VisualInfo *vi = NULL;
  71. struct Gadget *dummygg = NULL;
  72. struct Window *mywindow = NULL;
  73. struct MinList Header1;
  74. struct MinList Header2;
  75. struct RDArgs *rdargs = NULL;
  76. LONG Kommandoparameter[] = {
  77.           (ULONG)DEFAULTIN,
  78.           (ULONG)DEFAULTOUT,
  79.           (ULONG)DEFAULTLEFT,
  80.           (ULONG)DEFAULTRIGHT};
  81.  
  82. APTR mempool;
  83.  
  84. int FontHeight;
  85. struct TextAttr normalfont;
  86.  
  87. struct NewGadget gadget1=
  88.             {
  89.              5,15,200,150,NULL,&normalfont,1,PLACETEXT_BELOW,0,0
  90.             };
  91. struct NewGadget gadget2=
  92.             {
  93.              210,15,200,150,NULL,&normalfont,2,PLACETEXT_BELOW,0,0
  94.             };
  95.  
  96.  
  97. int main()
  98. {
  99.  static struct TagItem wintags[]=
  100.                 {
  101.                  WA_Flags,WFLG_SIZEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_ACTIVATE,
  102.                  WA_Left,0,
  103.                  WA_Top,15,
  104.                  WA_MinWidth,200,
  105.                  WA_MinHeight,70,
  106.                  WA_MaxWidth,-1,
  107.                  WA_MaxHeight,-1,
  108.                  WA_Title,(ULONG)"JukeBox Programming Function",
  109.                  WA_Width,640,
  110.                  WA_Height,184,
  111.                  WA_IDCMP,IDCMP_CLOSEWINDOW|LISTVIEWIDCMP|IDCMP_NEWSIZE,
  112.                  TAG_DONE,0
  113.                 };
  114.  struct IntuiMessage *imsg;
  115.  struct Gadget *address;
  116.  ULONG imClass;
  117.  UWORD imCode;
  118.  
  119.  if((mempool=LibCreatePool(MEMF_CLEAR,POOLSIZE,TRESHSIZE)) == NULL)
  120.   return;
  121.  
  122.  NewList((struct List *)&Header1);
  123.  gg1tags[0].ti_Data=((ULONG)&Header1);
  124.  NewList((struct List *)&Header2);
  125.  gg2tags[0].ti_Data=((ULONG)&Header2);
  126.  
  127.  if((DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",37L)))
  128.   {
  129.    if((GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L)))
  130.     {
  131.      if((IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L)))
  132.       {
  133.        if((GadToolsBase=OpenLibrary("gadtools.library",37L)))
  134.     {
  135.      /* hier sind alle Libraries offen */
  136.      rdargs=ReadArgs((STRPTR)"IN,OUT,LEFTTEXT,RIGHTTEXT",Kommandoparameter,NULL);
  137.      gadget1.ng_GadgetText=(UBYTE *)Kommandoparameter[2];
  138.      gadget2.ng_GadgetText=(UBYTE *)Kommandoparameter[3];
  139.      
  140.      if((mywindow=OpenWindowTagList(NULL,wintags)))
  141.       {
  142.        /* Verzweifelter Versuch, den richtigen Font zu nehmen */
  143.            FontHeight = mywindow->IFont->tf_YSize;
  144.            normalfont.ta_YSize = FontHeight;
  145.            normalfont.ta_Name = mywindow->IFont->tf_Message.mn_Node.ln_Name;
  146.            normalfont.ta_Style = mywindow->IFont->tf_Style;
  147.            normalfont.ta_Flags = mywindow->IFont->tf_Flags;
  148.            
  149.            /* daran ändert sich nix... */
  150.            gadget1.ng_LeftEdge = mywindow->BorderLeft+5;
  151.            gadget1.ng_TopEdge  = mywindow->BorderTop+5;
  152.            gadget2.ng_TopEdge  = gadget1.ng_TopEdge;
  153.  
  154.        AdjustGadgets();
  155.        if((MakeList()))
  156.         {
  157.           if((MakeGadgets()))
  158.           {
  159.            for(;;)
  160.         {
  161.          if(Wait(1L<<mywindow->UserPort->mp_SigBit|SIGBREAKF_CTRL_C)&SIGBREAKF_CTRL_C)
  162.           break;
  163.          while((imsg=GT_GetIMsg(mywindow->UserPort)) != NULL)
  164.           {
  165.            imClass = imsg->Class;
  166.            imCode  = imsg->Code;
  167.            address = imsg->IAddress;
  168.            GT_ReplyIMsg(imsg);
  169.            switch(imClass)
  170.             {
  171.              case IDCMP_CLOSEWINDOW:
  172.               goto ende;
  173.              case IDCMP_GADGETUP:
  174.               if(address->GadgetID==1)
  175.                {
  176.                 AddNodeRight(imCode);
  177.                 break;
  178.                }
  179.               if(address->GadgetID==2)
  180.                {
  181.                 RemNodeRight(imCode);
  182.                 break;
  183.                }
  184.              case IDCMP_NEWSIZE:
  185.               RemoveGadgets();
  186.               AdjustGadgets();
  187.               MakeGadgets();
  188.              default:
  189.               break;
  190.             }
  191.           }
  192.         }
  193.     ende:
  194.            RemoveGadgets();
  195.            WriteNumsToFile();
  196.           }
  197.          else
  198.           {
  199.            ShowMsg("Konnte Gadgets nicht anlegen!\n");
  200.           }
  201.         }
  202.        else
  203.         {
  204.          ShowMsg("Konnte Listen nicht erstellen!\n");
  205.         }
  206.        CloseWindow(mywindow);
  207.       }
  208.           FreeArgs(rdargs);
  209.      /* alle Libraries schließen */
  210.      CloseLibrary(GadToolsBase);
  211.     }
  212.        else
  213.         {
  214.          ShowMsg("Kann gadtools.library nicht öffnen!\n");
  215.         }
  216.        CloseLibrary((struct Library *)IntuitionBase);
  217.       }
  218.      else
  219.       {
  220.        ShowMsg("Kann intuition.library nicht öffnen!\n");
  221.       }
  222.      CloseLibrary((struct Library *)GfxBase);
  223.     }
  224.    else
  225.     {
  226.      ShowMsg("Kann graphics.library nicht öffnen!\n");
  227.     }
  228.    CloseLibrary((struct Library *)DOSBase);
  229.   }
  230.   LibDeletePool(mempool);
  231.  return 0;
  232. }
  233.  
  234. /* -------------------------------------- */
  235. /*            UNTERROUTINEN               */
  236. /* -------------------------------------- */
  237.  
  238.  
  239. void WriteNumsToFile(void)
  240.  {
  241.   BPTR filehandle = NULL;
  242.   char fmt[] = "%u\n";
  243.   struct Node *node;
  244.   UWORD args[]={0};
  245.     
  246.   if((filehandle = Open((char *)Kommandoparameter[1],MODE_NEWFILE)))
  247.    {
  248.     node = (struct Node *)Header2.mlh_Head;
  249.     while(node != (struct Node *)&Header2.mlh_Tail)
  250.      {
  251.       args[0] = node->ln_Type;
  252.       VFPrintf(filehandle,fmt,args);
  253.       node = node->ln_Succ;
  254.      }
  255.     Close(filehandle);
  256.    }
  257.   else
  258.    {
  259.     ShowMsg("Kann \"");
  260.     ShowMsg((char *)Kommandoparameter[1]);
  261.     ShowMsg("\"nicht öffnen!\n");
  262.    }
  263.  }
  264.  
  265.  
  266. struct Node *FindNodeNum(struct List * liste,UWORD Zahl)
  267.  {
  268.   struct Node * foundnode = NULL;
  269.   UWORD i;
  270.   if (IsListEmpty(liste))
  271.    return(foundnode);
  272.   foundnode = liste->lh_Head;
  273.   for(i=0; i<Zahl;i++)
  274.    {
  275.     if(foundnode->ln_Succ == NULL)
  276.      break;
  277.     foundnode = foundnode->ln_Succ;
  278.    } 
  279.   return(foundnode);
  280.  }
  281.  
  282. void AddNodeRight(UWORD Punkt)  /* Fügt eine Node (nummer x) ins rechte Gadget ein */
  283.  {
  284.   UBYTE tmpnum;
  285.   struct Node * nodetoclone = NULL;
  286.   if((nodetoclone = FindNodeNum((struct List *)&Header1,Punkt)))
  287.    {
  288.     tmpnum = nodetoclone->ln_Type;
  289.     nodetoclone = MakeNode(nodetoclone->ln_Name);
  290.     if((nodetoclone))
  291.      {
  292.       nodetoclone->ln_Type = tmpnum;
  293.       gg2tags[0].ti_Data=0L;
  294.       ChangeRightGadget();
  295.       AddTail((struct List *)&Header2,nodetoclone);
  296.       gg2tags[1].ti_Data++;
  297.       gg2tags[0].ti_Data=(ULONG)&Header2;
  298.       ChangeRightGadget();
  299.      }
  300.    }
  301.  }
  302.  
  303. void RemNodeRight(UWORD Punkt)
  304.  {
  305.   struct Node * nodetoremove = NULL;
  306.   if((nodetoremove = FindNodeNum((struct List *)&Header2,Punkt)))
  307.    {
  308.     gg2tags[0].ti_Data=0L;
  309.     ChangeRightGadget();
  310.     Remove(nodetoremove);
  311.     gg2tags[1].ti_Data--;
  312.     gg2tags[0].ti_Data=(ULONG)&Header2;
  313.     ChangeRightGadget();
  314.     LibFreePooled(mempool,nodetoremove,sizeof(struct Node)+strlen(nodetoremove->ln_Name)+1);
  315.    }
  316.  }
  317.  
  318. void ChangeRightGadget(void)
  319.  {
  320.   GT_SetGadgetAttrsA(gg2ptr,mywindow,NULL,gg2tags);
  321.  }
  322.  
  323. void AdjustGadgets(void)
  324.  {
  325.   gadget1.ng_Height   = mywindow->Height - mywindow->BorderBottom - FontHeight - 3 - gadget1.ng_TopEdge;
  326.   if(gadget1.ng_Height < 5)
  327.    {
  328.     gadget1.ng_Height = 5;
  329.    }
  330.   gadget2.ng_Height = gadget1.ng_Height;
  331.   gadget1.ng_Width    = ( (mywindow->Width - mywindow->BorderLeft - mywindow-> BorderRight - 15) / 2);
  332.    if(gadget1.ng_Width < 10)
  333.     {
  334.      gadget1.ng_Width = 10;
  335.     }
  336.   gadget2.ng_LeftEdge = gadget1.ng_Width + gadget1.ng_LeftEdge + 5;
  337.   gadget2.ng_Height   = gadget1.ng_Height;
  338.   gadget2.ng_Width    = gadget1.ng_Width;
  339.  }
  340.  
  341. /* Muß hier Gadgets belegen und einbinden. */
  342.  
  343. int MakeGadgets(void)
  344.  {
  345.  
  346.   int ret=FALSE;
  347.  
  348.   if((vi=GetVisualInfoA(mywindow->WScreen,NULL)))
  349.    {
  350.  
  351.     gadget1.ng_VisualInfo = vi;
  352.     gadget2.ng_VisualInfo = vi;
  353.  
  354.     if((dummygg=CreateContext(&glistptr)))
  355.      {
  356.       if((gg1ptr=CreateGadgetA(LISTVIEW_KIND,dummygg,&gadget1,gg1tags)))
  357.        {
  358.     if((gg2ptr=CreateGadgetA(LISTVIEW_KIND,gg1ptr,&gadget2,gg2tags)))
  359.      {
  360.       ret=TRUE;
  361.       EraseRect(mywindow->RPort,mywindow->BorderLeft,mywindow->BorderTop,mywindow->Width-mywindow->BorderRight-1,mywindow->Height-mywindow->BorderBottom-1);
  362.       AddGList(mywindow,glistptr,-1,-1,0);
  363.       RefreshWindowFrame(mywindow);
  364.       GT_RefreshWindow(mywindow,0);
  365.      }
  366.     else
  367.      {
  368.       FreeGadgets(glistptr);
  369.       glistptr = NULL;
  370.      }
  371.        }
  372.      }
  373.     else
  374.      {
  375.       FreeVisualInfo(vi);
  376.      }
  377.    }
  378.   return(ret);
  379.  }
  380.  
  381. void RemoveGadgets(void)
  382.  {
  383.   if((glistptr))
  384.    {
  385.     RemoveGList(mywindow,glistptr,-1);
  386.     FreeGadgets(glistptr);
  387.     FreeVisualInfo(vi);
  388.    }
  389.  }
  390.  
  391. int MakeList(void)
  392.  {
  393.   struct FileInfoBlock *fib = NULL;
  394.   struct Node *node=NULL;
  395.   BPTR filehandle = NULL;
  396.   BYTE *buffer;
  397.   BYTE *tempptr;
  398.   UBYTE TrackNum = 1;
  399.   int ret = FALSE;
  400.   LONG pos=0;
  401.  
  402.   if((fib=AllocDosObject(DOS_FIB,NULL)))
  403.    {
  404.     if((filehandle=Open((char *)Kommandoparameter[0],MODE_OLDFILE)))
  405.      {
  406.       if(DOSTRUE==ExamineFH(filehandle,fib))
  407.        {
  408.         if((buffer=LibAllocPooled(mempool,fib->fib_Size)))
  409.          {
  410.            Read(filehandle,buffer,fib->fib_Size);
  411.           /* Eigentliche Bearbeitung der Textdatei */
  412.        tempptr = buffer;
  413.        while(tempptr < buffer+fib->fib_Size) /* solange nicht über Ende hinaus*/
  414.         {
  415.          if((node=MakeNode((char *)tempptr)))
  416.           {
  417.            node->ln_Type = TrackNum;   /* Mißbraucht um die Tracknummer zu sichern! */
  418.            TrackNum++;
  419.            AddTail((struct List *)&Header1,node);
  420.           }
  421.          for(;tempptr[0] != 0;tempptr++)
  422.           {
  423.            /* Nix! */
  424.           }
  425.          tempptr++;
  426.         }
  427.         ret=TRUE;
  428.       LibFreePooled(mempool,buffer,fib->fib_Size);
  429.          }
  430.         else
  431.          {
  432.           ShowMsg(NOMEM);
  433.          }
  434.        }
  435.       else
  436.        {
  437.         ShowMsg("Examine fehlgeschlagen.\n");
  438.        }
  439.       Close(filehandle);
  440.      }
  441.     else
  442.      {
  443.       ShowMsg("Konnte \"");
  444.       ShowMsg((char *)Kommandoparameter[0]);
  445.       ShowMsg("\" nicht öffnen!\n");
  446.      }
  447.     FreeDosObject(DOS_FIB,fib);
  448.    }
  449.   else
  450.    {
  451.     ShowMsg("Konnte FileInfoBlock nicht belegen!\n");
  452.    }
  453.   return(ret);
  454.  }
  455.  
  456. /* Erzeugt stinknormale Nodes (mit Name dahinter, natürlich! */
  457.  
  458. struct Node *MakeNode(char string[])
  459.  {
  460.   struct Node *mem;
  461.  
  462.   if((mem=LibAllocPooled(mempool,sizeof(struct Node)+strlen(string)+1)))
  463.    {
  464.     mem->ln_Name=(char *)(mem+1);
  465.     strcpy((char *)(mem+1),string);
  466.    }
  467.   else
  468.    {
  469.     ShowMsg(NOMEM);
  470.    }
  471.   return(mem);
  472.  }
  473.  
  474. /* gibt nur ne Meldung aus ;) */
  475.  
  476. void ShowMsg(STRPTR msg)
  477.  {
  478.   PutStr(msg);
  479.  }
  480.  
  481.  
  482.